package com.mi.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mi.entity.*;
import com.mi.service.ProductCategoryService;
import com.mi.service.ProductService;
import com.mi.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductController {

    @Reference(check = false)
    ProductService productService;

    @Reference(check = false)
    ProductCategoryService productCategoryService;

    //获取商品点击次数
    @RequestMapping("getCheckNumber")
    @ResponseBody
    public CheckEntity getCheckNumber(){
        List<ProductEntity> checkNumber = productService.getCheckNumber();
        CheckEntity checkEntity = new CheckEntity();
        ArrayList<Integer> checkn = new ArrayList<Integer>();
        ArrayList<Integer> ranking = new ArrayList<Integer>();
        ArrayList<String> names = new ArrayList<String>();
        for (int i = 0; i < checkNumber.size(); i++) {
            checkn.add(checkNumber.get(checkNumber.size()-1-i).getP_check());
            ranking.add(checkNumber.size()-i);
            names.add(checkNumber.get(checkNumber.size()-1-i).getP_name());
        }
        checkEntity.setCheckNumber(checkn);
        checkEntity.setRanking(ranking);
        checkEntity.setName(names);
        return checkEntity;
    }

    //产品列表
    @RequestMapping("productList")
    public String productList(Integer ppc_id,ProductEntity productEntity, HttpServletRequest request){
        MyPages<ProductEntity> productEntityMyPages = productService.queryProductss(productEntity);
        List<ProductCategoryEntity> productCategoryEntities = productCategoryService.queryProductsRank();
        request.setAttribute("pageInfo",productEntityMyPages);
        request.setAttribute("productCategorys",productCategoryEntities);
        request.setAttribute("p_name",productEntity.getP_name());
        request.setAttribute("pc_id",productEntity.getPc_id());
        request.setAttribute("ppc_id",ppc_id);
        return "productList";
    }

    //跳转到详情界面
    @RequestMapping("productDetails")
    public String productDetails(String p_id,HttpServletRequest request){
        if(p_id==null||p_id==""){
            return "error";
        }
        ProductEntity productEntity = productService.queryProduct(Integer.valueOf(p_id));
        request.setAttribute("product",productEntity);
        return "productDetails";
    }

    //产品分类查找
    @RequestMapping("productMin")
    @ResponseBody
    public List<ProductCategoryEntity> productMin(String ppc_id){
        if(ppc_id==null||ppc_id==""){
            return null;
        }
        ProductCategoryEntity productCategoryEntity = new ProductCategoryEntity();
        productCategoryEntity.setPc_id(Integer.valueOf(ppc_id));
        List<ProductCategoryEntity> productCategoryEntities = productCategoryService.queryProductsInfo(productCategoryEntity);
        return productCategoryEntities;
    }

    //删除产品
    @RequestMapping("delProduct")
    @ResponseBody
    public ResponseMsg delProduct(String ids){
        ResponseMsg rm = new ResponseMsg();
        String[] p_ids;
        if (ids.contains(",")) {
            p_ids = ids.split(",");
        } else {
            p_ids = new String[]{ids};
        }
        boolean b = productService.delProduct(p_ids);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //修改产品状态
    @RequestMapping("updProductStatus")
    @ResponseBody
    public ResponseMsg updProductStatus(ProductEntity productEntity){
        ResponseMsg rm = new ResponseMsg();
        boolean b = productService.updProductStatus(productEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到修改产品界面
    @RequestMapping("productEdit")
    public String productEdit(ProductEntity productEntity,HttpServletRequest request){
        ProductEntity productEntity1 = productService.queryProduct(productEntity);
        if(productEntity1 != null){
            List<ProductCategoryEntity> productCategoryEntities = productCategoryService.queryProductsRank();
            request.setAttribute("productCategorys",productCategoryEntities);
            request.setAttribute("product",productEntity1);
            return "productEdit";
        }else{
            return "error";
        }
    }

    //修改产品
    @RequestMapping("updProduct")
    @ResponseBody
    public ResponseMsg updProduct(String descc,String trait,MultipartFile myfile, ProductEntity productEntity, HttpServletRequest request){
        ResponseMsg rm = new ResponseMsg();
        File file;
        FileOutputStream fos = null;
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String productImage = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productIcon";
            file = new File(productImage);
            if(!file.exists()){
                file.mkdirs();
            }
            productImage = productImage + File.separator+ UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                fos = new FileOutputStream(productImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                is.close();
                productEntity.setP_image(File.separator+"admin"+File.separator+productImage.substring(productImage.lastIndexOf(File.separator,productImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(descc!=null&&descc!=""){
            String productDesc = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productDesc";
            file = new File(productDesc);
            if(!file.exists()){
                file.mkdirs();
            }
            productDesc = productDesc+File.separator+UUIDUtil.getUUID()+".html";
            try {
                fos = new FileOutputStream(productDesc);
                FileCopyUtils.copy(descc.getBytes(),fos);
                productEntity.setP_desc(File.separator+"admin"+File.separator+productDesc.substring(productDesc.lastIndexOf(File.separator,productDesc.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(descc!=null&&descc!=""){
            String productTrait = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productTrait";
            productTrait = productTrait+File.separator+UUIDUtil.getUUID()+".html";
            file = new File(productTrait);
            if(!file.exists()){
                file.mkdirs();
            }
            try {
                fos = new FileOutputStream(productTrait);
                FileCopyUtils.copy(trait.getBytes(),fos);
                productEntity.setP_trait(File.separator+"admin"+File.separator+productTrait.substring(productTrait.lastIndexOf(File.separator,productTrait.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fos!=null){
            fos = null;
        }
        boolean b = productService.updProduct(productEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到添加产品界面
    @RequestMapping("productAdd")
    public String productAdd(HttpServletRequest request){
        List<ProductCategoryEntity> productCategoryEntities = productCategoryService.queryProductsRank();
        request.setAttribute("productCategorys",productCategoryEntities);
        return "productAdd";
    }

    //添加产品
    @RequestMapping("addProduct")
    @ResponseBody
    public ResponseMsg addProduct(String descc,String trait,MultipartFile myfile,ProductEntity productEntity){
        ResponseMsg rm = new ResponseMsg();
        File file;
        FileOutputStream fos = null;
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String productImage = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productIcon";
            file = new File(productImage);
            if(!file.exists()){
                file.mkdirs();
            }
            productImage = productImage + File.separator+ UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                fos = new FileOutputStream(productImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                is.close();
                productEntity.setP_image(File.separator+"admin"+File.separator+productImage.substring(productImage.lastIndexOf(File.separator,productImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(descc!=null&&descc!=""){
            String productDesc = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productDesc";
            file = new File(productDesc);
            if(!file.exists()){
                file.mkdirs();
            }
            productDesc = productDesc+File.separator+UUIDUtil.getUUID()+".html";
            try {
                fos = new FileOutputStream(productDesc);
                FileCopyUtils.copy(descc.getBytes(),fos);
                productEntity.setP_desc(File.separator+"admin"+File.separator+productDesc.substring(productDesc.lastIndexOf(File.separator,productDesc.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(trait!=null&&trait!=""){
            String productTrait = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productTrait";
            file = new File(productTrait);
            if(!file.exists()){
                file.mkdirs();
            }
            productTrait = productTrait+File.separator+UUIDUtil.getUUID()+".html";
            try {
                fos = new FileOutputStream(productTrait);
                FileCopyUtils.copy(trait.getBytes(),fos);
                productEntity.setP_trait(File.separator+"admin"+File.separator+productTrait.substring(productTrait.lastIndexOf(File.separator,productTrait.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fos!=null){
            fos = null;
        }
        boolean regist = productService.addProduct(productEntity);
        if (regist) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("添加失败");
        }
        return rm;
    }

}
